<?php

include('../config.php');

$log = new Logging();

$log->lfile('../logs/operation_with_user_notification_' . date('Y_m_d') . '.log');
$log->lwrite('Start');

$db_obj = new db_obj();
$task_obj = new task_obj();
$notify_obj = new notify_obj();

$filter = array();
$filter['date_of_action'] = date("Y-m-d");
$operations = $task_obj->getOperationsWithUser($filter);

if (!empty($operations)){
    foreach ($operations as $key => $value){
        $task = $task_obj->getTaskById($value['taskId']);
        if (!error_obj::isError($task)){
            $sql = "SELECT ".$GLOBALS['tb_users'].".email FROM " .$GLOBALS['tb_clients']. ", " .$GLOBALS['tb_users']. " 
                WHERE ".$GLOBALS['tb_clients'].".klient_id='".$task['client']."' 
                AND ".$GLOBALS['tb_users'].".users_id=".$GLOBALS['tb_clients'].".contactPerson";
            $kzoAC = $db_obj->getOne($sql);
            
            $sql = "SELECT ".$GLOBALS['tb_users'].".email FROM " .$GLOBALS['tb_users']. " 
                WHERE ".$GLOBALS['tb_users'].".client='".$task['client']."' AND userlevel='".USER_TYPE_CLIENT."'";
            $kos = $db_obj->getCol($sql);
            
            if (!error_obj::isError($kzoAC) && !error_obj::isError($kos)){
                $recipients = $kos;
                $recipients[] = $kzoAC;
                $message = 'Today it is necessary to perform the operation with the user from task <a href="'.WWW.'?input=edit_task&task='.$value['taskId'].'">'.$task['title'].'</a>.';
                $subject = 'AlfaCM: Time to perform operation with user';
                
                $notify_obj->send($recipients, $message, $subject);
                
            }
        }
    }
}

$log->lwrite('Stop');
$log->lwrite('------------------------------------------');

$log->lclose();
?>
